import errorEvent from '../../errorMessage';
import store from '@/store/index';
import httpService from '../../httpService';

export default {
    async getAll() {
        var projectId = store.state.mainOptions.project.projectId;
        var serviceAddress = store.state.mainOptions.service;
        try {
            var data = await httpService({
                type: 'get',
                url: 'http://' + serviceAddress + '/model',
                params: {
                    projectId: projectId,
                }
            });
            return data;
        } catch (error) {
            errorEvent(error);
            return false;
        }

    },
    async creat(param) {


        var projectId = store.state.mainOptions.project.projectId;
        var serviceAddress = store.state.mainOptions.service;
        try {
            var data = await httpService({
                type: 'post',
                url: 'http://' + serviceAddress + '/model',
                headers: { token: store.state.mainOptions.token },
                params: { projectId: store.state.mainOptions.project.projectId, },
                data: {
                    projectId: projectId,
                    text: param.text,
                    key: param.key,
                    name: param.name,
                }
            });
            return data;
        } catch (error) {
            errorEvent(error);
            return false;
        };
    },
    async update(param) {


        var serviceAddress = store.state.mainOptions.service;
        try {
            var data = await httpService({
                type: 'put',
                url: 'http://' + serviceAddress + '/model',
                headers: { token: store.state.mainOptions.token },
                params: { projectId: store.state.mainOptions.project.projectId, },
                data: {
                    modelId: param.id,
                    text: param.text,
                    key: param.key,
                    name: param.name,
                }
            });
            return data;
        } catch (error) {
            errorEvent(error);
            return false;
        };
    },
    async delete(id) {
        var serviceAddress = store.state.mainOptions.service;
        try {
            var data = await httpService({
                type: 'delete',
                url: 'http://' + serviceAddress + '/model',
                headers: { token: store.state.mainOptions.token },
                params: { projectId: store.state.mainOptions.project.projectId, },
                data: {
                    modelId: id,
                }
            });
            return data;
        } catch (error) {
            errorEvent(error);
            return false;
        };
    },
}